<?php namespace Phpcmf;

use \Phpcmf\View;

/**
 * OA操作类
 */
class OA
{
	/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
		
	public function handle()
    {
        $this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);
    }
	
		/**

	 * 初始调用方法

	 *

	 * @return void

	 */
	public function init(){}

	/*

	 * 重载父类Smarty类中的方法

	 * @param	string	$resource_name	模板的位置

	 * @param	mixed	$cache_id	缓存的ID

	 */

	public function display($resource_name = null, $cache_id = null, $compile_id = null, $parent = null) {

		//将部分全局变量直接分配到模板中使用

		/**

		$this->assign("root", B_ROOT);

		$this->assign("app",B_APP);

		$this->assign("url", B_URL);

		$this->assign("public", B_PUBLIC);

		$this->assign("res", B_RES);

		 * */

		if(is_null($resource_name)){
			$resource_name = Route::getTplPath().".".$this->config['tpl_prefix'];
			if ($this->method){
				$resource_name = str_replace("callmethod",$this->method,$resource_name);
			}

			if(is_file($resource_name)){
				parent::display($resource_name, $cache_id, $compile_id, $parent);
			}else{
				throw new \UnexpectedValueException("视图文件".$resource_name."不存在！");
			}
		}else{
			if(is_file($resource_name)){
				parent::display($resource_name, $cache_id, $compile_id, $parent);
			}else{
				throw new \UnexpectedValueException("视图文件".$resource_name."不存在！");
			}
		}

	}

	/*

		 * 重载父类的Smarty类中的方法

		 * @param	string	$tpl_file	模板文件

		 * @param	mixed	$cache_id	缓存的ID

		 */
	public function isCached($tpl_file=null, $cache_id = null, $compile_id = null, $parent = null) {
		if(is_null($tpl_file)){
			$tpl_file = Route::getTplPath().".".$this->config['tpl_prefix'];
			if ($this->method){
				$tpl_file = str_replace("callmethod",$this->method,$tpl_file);
			}
		}
		return parent::isCached($tpl_file, $cache_id, $compile_id, $parent);
	}

	/*

	 * 重载父类的Smarty类中的方法

	 *  @param	string	$tpl_file	模板文件

	 * @param	mixed	$cache_id	缓存的ID

	 */
	public function clearCache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null, $type = null){
		if(is_null($tpl_file)){
			$tpl_file = Route::getTplPath().".".$this->config['tpl_prefix'];
			if ($this->method){
				$tpl_file = str_replace("callmethod",$this->method,$tpl_file);
			}
		}
		return parent::clearCache($tpl_file, $cache_id, $compile_id, $exp_time);
	}

	/*

	 * 初始化路由callMethod

	 */

	public function callMethod($method='index',$parameter=0){

		if (method_exists($this,$method)){

			$this->method = $method;

			$parameter = $parameter?$parameter:'';

			$this->$method($parameter);

		}else{

			echo 'method '. $method .' is not exist!';

		}

	}


	public function callMethod(){

		$parameterArr = func_get_args();	// 取得参数，并转化为数组

		$parameterNum = func_num_args();	// 取得参数数量

		$parameters = '';



		if ($parameterNum==0){

			$parameterArr[0] = 'index';	// 方法参数为空，默认加载 index() 方法

		}else{

			for ($i=1; $i<$parameterNum; $i++){

				$parameters .= ','.$parameterArr[$i];

			}

			$parameters = substr($parameters, 1);	// ",7,6" 改为: "7,6" 去掉前面的逗号

		}

		$this->method = $parameterArr[0];



		if (method_exists($this,$parameterArr[0])){

			$this->$parameterArr[0]($parameters);

		}else{

			echo 'method '. $parameterArr[0] .' is not exist!';

		}


		public function callMethod(){
			$parameterArr = func_get_args();	// 取得参数，并转化为数组
			$parameterNum = func_num_args();	// 取得参数数量
			$parameters = '';

			if ($parameterNum == 2){	//	Request::capture(), $match['params'] 引入
				$parameterArr = $parameterArr[1];	// $match['params'] 参数
				$parameterNum = count($parameterArr);
			}

			if ($parameterNum==0){
				$parameterArr[0] = 'index';	// 方法参数为空，默认加载 index() 方法
			}else{
				foreach ($parameterArr as $key=>$value){
					$parameters .= ','.$value;
				}
				$parameterArr[0] = current($parameterArr);

				$parameters = substr($parameters, 1);	// ",7,6" 改为: "7,6" 去掉前面的逗号

				$start = strpos($parameters,',');
				if ($start != 0){
					$parameters = substr($parameters,$start+1);
				}else{
					$parameters = '';
				}
			}

			$this->method = $parameterArr[0];

			if (method_exists($this,$parameterArr[0])){
				/**

				if ($parameters==''){

					$this->$parameterArr[0](Request::capture());

				}else{

					$this->$parameterArr[0](Request::capture(),$parameters);

				}

				 * */
				$this->$parameterArr[0](Request::capture(),$parameters);

			}else{
				echo 'method '. $parameterArr[0] .' is not exist!';
			}
	}
}